package com.fjut.kms.service;

import com.fjut.kms.dao.UserDao;
import com.fjut.kms.dao.UserDaoJDBCImpl;
import com.fjut.kms.domain.User;
import com.fjut.kms.exception.KMSException;

/**
 * Created by Empathy on 2017/7/12.
 * update 8/23,
 */
public class UserServiceImpl implements UserService{
    @Override
    public User checkUser(String userName, String userPwd) {

        UserDao userDao = new UserDaoJDBCImpl();

        User user = userDao.getUserByName(userName);

        if(!user.getUserStatus().equals("正常"))
            throw  new KMSException("该用户当前状态不可登陆");
        if(!user.getUserPass().equals(userPwd))
            throw new KMSException("用户名或登录密码不正确!");

        //  userDao.updateLoginTimes(userName); //改用触发器实现
        return user;
    }

    public void addLoginRecord(User user){
        UserDao userDao=new UserDaoJDBCImpl();
        userDao.addLoginRecord(user);
    }

    public void isRegister(User user){
        UserDao userDao=new UserDaoJDBCImpl();
        UserDepartmentService udService=new UserDepartmentServiceImpl();
        if (userDao.getUserNameByUserName(user.getUserName())!=null)
            throw  new KMSException("用户名已存在！");
        //客户端保证
        // if (!udService.isExist(user.getDepyName()))
        //   throw new KMSException("部门不存在！");
        user.setUserStatus("注册");
        userDao.addUser(user);
    }
    public void modifyPassword(String userName,String oldPass,String newPass){
        UserDao userDao=new UserDaoJDBCImpl();
        checkUser(userName,oldPass);
        userDao.updatePassByUserName(userName,newPass);
    }
    public String getUserDept(String userName)
    {
        UserDao userDao=new UserDaoJDBCImpl();
        String ret=userDao.getDept(userName);
        return ret;
    }
}
